﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using tracker.Models;
using tracker.Models.DTO;
using tracker.Models.BLL;
using System.Data;

namespace tracker.Controllers
{
    public class HandlerADUDriverController : Controller
    {
        /// <summary>
        /// GET HandlerADUDriver 
        /// </summary>
        /// <param name="strAlarmType">Alarm Type</param>
        /// <param name="strExpression">Expression</param>
        /// <param name="strHandlerType">Add/Update</param>
        /// <returns>true/false</returns>
        [CallAjaxFilter]
        public string Index(string driver_id, 
                            string controlType, 
                            string driver_age, 
                            string driver_cellphone, 
                            string driver_email,
                            string driver_name,
                            string driver_phone,
                            string driver_remarks)
        {
            string _username = HttpContext.Session[Common.USER].ToString();
            string _language = HttpContext.Session[Common.LANGUAGE].ToString();
            _language = _language == string.Empty ? "vi" : _language;

            // Get information of current user
            DataTable dtGetUser = UserLoginBLL.SelectByUserName(new string[] { EUserLogin.FIELD_USERNAME },
                                                                new object[] { _username }).Tables[0];

            if (dtGetUser.Rows.Count > 0)
            {
                string type = controlType;
                if (type == "add" || type== "update")
                {
                    if (ClassCommon.classValidate.isDateTime(driver_age))
                    {
                        EDriverInfo ent = new EDriverInfo();

                        ent.Birthday = DateTime.Parse(driver_age);
                        ent.Cellphone = driver_cellphone;
                        ent.CreateBy = _username;
                        ent.DriverName = driver_name;
                        ent.Email = driver_email;
                        ent.Phone = driver_phone;
                        ent.Remark = driver_remarks;
                        ent.ModifyDate = DateTime.Now;
                        ent.ModifyBy = _username;

                        ent.Status = 1;

                        if (type == "add")
                        {
                            string strResult = DriverInfoBLL.InsertERP(new string[]{ ENTDriverInfo.FIELD_DRIVERNAME,
                                                                                     ENTDriverInfo.FIELD_BIRTHDAY,
                                                                                     ENTDriverInfo.FIELD_EMAIL,
                                                                                     ENTDriverInfo.FIELD_PHONE,
                                                                                     ENTDriverInfo.FIELD_CELLPHONE,
                                                                                     ENTDriverInfo.FIELD_REMARK,
                                                                                     ENTDriverInfo.FIELD_CREATEBY,
                                                                                     ENTDriverInfo.FIELD_STATUS
                                                                       },
                                                                       new object[]{ ent.DriverName,
                                                                                     ent.Birthday,
                                                                                     ent.Email,
                                                                                     ent.Phone,
                                                                                     ent.Cellphone,
                                                                                     ent.Remark,
                                                                                     ent.CreateBy,
                                                                                     ent.Status
                                                                       });
                            if (strResult == "0")
                                return "{\"success\":true}";
                            else
                                return "false";
                        }
                        else
                        {
                            if (ClassCommon.classValidate.isInt(driver_id))
                            {
                                ent.DriverID = int.Parse(driver_id);
                                string strResult = DriverInfoBLL.UpdateERP(new string[]{ ENTDriverInfo.FIELD_DRIVERID,
                                                                                         ENTDriverInfo.FIELD_DRIVERNAME,
                                                                                         ENTDriverInfo.FIELD_BIRTHDAY,
                                                                                         ENTDriverInfo.FIELD_EMAIL,
                                                                                         ENTDriverInfo.FIELD_PHONE,
                                                                                         ENTDriverInfo.FIELD_CELLPHONE,
                                                                                         ENTDriverInfo.FIELD_REMARK,
                                                                                         ENTDriverInfo.FIELD_MODIFYBY
                                                                           },
                                                                           new object[]{ ent.DriverID,
                                                                                         ent.DriverName,
                                                                                         ent.Birthday,
                                                                                         ent.Email,
                                                                                         ent.Phone,
                                                                                         ent.Cellphone,
                                                                                         ent.Remark,
                                                                                         ent.ModifyBy
                                                                           });
                                if (strResult == "0")
                                    return "{\"success\":true}";
                                else
                                    return "false";
                            }
                            else
                                return "false";
                        }

                    }
                }

                if (type == "delete")
                {
                    if (ClassCommon.classValidate.isInt(driver_id))
                    {
                        string strResult = DriverInfoBLL.DeleteERP(new string[] { ENTDriverInfo.FIELD_DRIVERID, EAlarmDefine.FIELD_CREATEBY },
                                                                   new object[] { int.Parse(driver_id), _username });

                        if (strResult == "0")
                            return "{\"success\":true}";
                        else
                            return "false";
                    }
                    else
                        return "false";
                }
                else
                    return "false";
            }
            else
                return "false";
        }
    }
}
